package com.hrms.hrms_employee.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrms.common.utils.DateUtil;
import com.hrms.common.utils.GrowthRateCalculator;
import com.hrms.hrms_employee.dao.StationDao;
import com.hrms.hrms_employee.entity.StaffPosition;
import com.hrms.hrms_employee.entity.Station;
import com.hrms.hrms_employee.service.StationService;
import com.hrms.hrms_employee.vo.EmployeeGrowthDTO;
import com.hrms.hrms_employee.vo.stationDetail;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.List;
import java.util.Map;

/**
 * 岗位表(Station)表服务实现类
 *
 * @author makejava
 * @since 2025-01-15 13:55:34
 */
@Service("stationService")
public class StationServiceImpl extends ServiceImpl<StationDao, Station> implements StationService {

    @Autowired
    private StationDao stationDao;
    @Override
    public Page<stationDetail> pageInfo(Page<stationDetail> page, Map<String, String> map) {
        Page<stationDetail> page1 = this.stationDao.pageInfo(page,map);
        return page1;
    }

    @Override
    public void saveOrUpdateInfo(stationDetail stationDetail) {
        Station station = new Station();
        BeanUtils.copyProperties(stationDetail,station);
        this.saveOrUpdate(station);
    }

    /**
     * 根据departmentId查询部门负责人
     *
     * @param
     * @param
     * @return 所有数据
     */
    @Override
    public List<StaffPosition> selectManagerByDepartmentId(Long stationId) {
        return this.stationDao.selectManagerByDepartmentId(stationId);
    }

    /**
     * 获取员工统计指标
     * @param targetMonth
     * @return
     */
    @Override
    public EmployeeGrowthDTO getGrowthStats(YearMonth targetMonth) {
        EmployeeGrowthDTO dto = new EmployeeGrowthDTO();
        DateUtil dateUtil = new DateUtil();
        LocalDate monthFirst = dateUtil.getMonthFirst(targetMonth);
        LocalDate monthLast = dateUtil.getMonthLast(targetMonth);
        //月初员工
        Integer startMonthCount = this.stationDao.countActiveEmployeesByDate(monthFirst);
        dto.setCurrentStartCount(startMonthCount);
        //月末员工
        Integer endMonthCount = this.stationDao.countActiveEmployeesByDate(monthLast);
        dto.setCurrentEndCount(endMonthCount);
        //新员工
        Integer newEmployees = this.stationDao.countNewEmployees(monthFirst,monthLast);
        dto.setCurrentNewEmployees(newEmployees);
        //离职员工
        Integer leftEmployees = this.stationDao.countLeftEmployees(monthFirst,monthLast);
        dto.setCurrentLeftEmployees(leftEmployees);
        //上月
        YearMonth prevMonth = targetMonth.minusMonths(1);
        LocalDate preMonthFirst = dateUtil.getMonthFirst(prevMonth);
        LocalDate preMonthLast = dateUtil.getMonthLast(prevMonth);
        //上月初员工
        Integer preStartMonthCount = this.stationDao.countActiveEmployeesByDate(preMonthFirst);
        dto.setPrevStartCount(preStartMonthCount);
        //上月末员工
        Integer preEndMonthCount = this.stationDao.countActiveEmployeesByDate(preMonthFirst);
        dto.setPrevEndCount(preEndMonthCount);
        //上月新员工
        Integer preNewEmployees = this.stationDao.countNewEmployees(preMonthFirst,preMonthLast);
        dto.setPrevNewEmployees(preNewEmployees);
        //上月离职员工
        Integer preLeftEmployees = this.stationDao.countLeftEmployees(preMonthFirst,preMonthLast);
        dto.setPrevLeftEmployees(preLeftEmployees);

        //增长率
        // 计算增长率（处理可能为null的情况）
        dto.setStartGrowthRate(calculateSafeGrowth(
                dto.getCurrentStartCount(),
                dto.getPrevStartCount() // 注意：本月月初=上月月末
        ));
        dto.setEndGrowthRate(calculateSafeGrowth(
                dto.getCurrentEndCount(),
                dto.getPrevEndCount() // 注意：本月月初=上月月末
        ));
        dto.setNewEmployeesGrowthRate(calculateSafeGrowth(
                dto.getCurrentNewEmployees(),
                dto.getPrevNewEmployees() // 注意：本月月初=上月月末
        ));
        dto.setLeftEmployeesGrowthRate(calculateSafeGrowth(
                dto.getCurrentLeftEmployees(),
                dto.getPrevLeftEmployees() // 注意：本月月初=上月月末
        ));
        return dto;
    }

    /**
     * 获取当前部门员工数量
     * @param name
     * @return
     */
    @Override
    public Integer seelectEmployee(String name) {
        return this.stationDao.selectEmployee(name);
    }

    //增长率
    private Double calculateSafeGrowth(int current, int previous) {
        try {
            return GrowthRateCalculator.calculate(current, previous);
        } catch (ArithmeticException e) {
            return null;
        }
    }
}

